﻿/*
 * @Author: xiaxia
 * @Description: 
 * @Date: 2023-04-13 17:04:07
 * @LastEditTime: 2024-05-23 10:20:07
 * @FilePath: \vue-admin-template\src\utils\LodopFuncs.js
 */
import { MessageBox } from 'element-ui'

//====判断是否需要安装CLodop云打印服务器:====
export function needCLodop() {
  try {
    var ua = navigator.userAgent
    if (ua.match(/Windows\sPhone/i) != null) return true
    if (ua.match(/iPhone|iPod/i) != null) return true
    if (ua.match(/Android/i) != null) return true
    if (ua.match(/Edge\D?\d+/i) != null) return true

    var verTrident = ua.match(/Trident\D?\d+/i)
    var verIE = ua.match(/MSIE\D?\d+/i)
    var verOPR = ua.match(/OPR\D?\d+/i)
    var verFF = ua.match(/Firefox\D?\d+/i)
    var x64 = ua.match(/x64/i)
    if (verTrident == null && verIE == null && x64 !== null) return true
    else if (verFF !== null) {
      verFF = verFF[0].match(/\d+/)
      if (verFF[0] >= 42 || x64 !== null) return true
    } else if (verOPR !== null) {
      verOPR = verOPR[0].match(/\d+/)
      if (verOPR[0] >= 32) return true
    } else if (verTrident == null && verIE == null) {
      var verChrome = ua.match(/Chrome\D?\d+/i)
      if (verChrome !== null) {
        verChrome = verChrome[0].match(/\d+/)
        if (verChrome[0] >= 42) return true
      }
    }
    return false
  } catch (err) {
    return true
  }
}

if (needCLodop()){
  // ====页面动态加载C-Lodop云打印必须的文件CLodopfuncs.js====
  var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement
  var oscript = document.createElement('script')
  // 让本机的浏览器打印(更优先一点)：
  oscript = document.createElement('script')
  oscript.src = 'http://localhost:8000/CLodopfuncs.js?priority=1'
  head.insertBefore(oscript, head.firstChild)
  // 加载双端口(8000和18000）避免其中某个端口被占用：
  oscript = document.createElement('script')
  oscript.src = 'http://localhost:18000/CLodopfuncs.js?priority=0'
  head.insertBefore(oscript, head.firstChild)
}

// 下载loadLodop
function loadLodop() {
  let agent = navigator.userAgent.toLowerCase()
  // 64位系统也可使用32位的控件，这里统一使用32位的控件 方便更新最新版本
  // let is64Bit = agent.indexOf('wow64') >= 0 || agent.indexOf('win64') >= 0
  // if(is64Bit) {  // 64位
    // window.open('../../static/Lodop/CLodop_Setup_for_Win64NT_6.571EN.exe')
  // } else {
    window.open('../../static/Lodop/CLodop_Setup_for_Win32NT.exe')
  // }
}

// ====获取LODOP对象的主过程：====
function getLodop() {
  var LODOP
  try {
    LODOP = getCLodop()
    if (!LODOP && document.readyState !== 'complete') {
      MessageBox.alert('C-Lodop打印控件还没准备好，请稍后再试！')
      return
    }
    LODOP.SET_LICENSES("ZCSB技术处研发中心", "545C44BD459DBBAFB79F18A0F8160A12", "", "")
    return LODOP
  } catch (err) {
    console.log(err, 'err');
    MessageBox({
      title: '温馨提示',
      type: 'warning',
      showCancelButton: true,
      confirmButtonText: '下载',
      cancelButtonText: '取消',
      message: '您还未安装打印控件，点击确定下载打印控件，安装成功后刷新页面即可进行打印',
      callback: res => {
        if (res === 'confirm') {
          loadLodop()
        }
      }
    })
  }
}

export  { getLodop }